package com.pulse.common.utils.sor;

import com.pulse.common.utils.sor.domain.Block;
import com.pulse.common.utils.sor.domain.SorInfo;
import com.pulse.common.utils.sor.domain.SupParamsVo;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class SupParams {
    /**
     * 处理 SupParams 块
     *
     * @param fh      输入流
     * @param sorInfo 存储结果的对象
     * @return 状态字符串
     */
    public static String process(BufferedInputStream fh, SorInfo sorInfo) {
        final String bname = "SupParams";
        int hsize = bname.length() + 1;  // 包括尾部的 '\0'
        Block refBlock = null;
        String status = "nok";

        try {
            // 获取块的引用
            refBlock = sorInfo.getBlocks().getSupParams();
            long startpos = (long) refBlock.getPos();
            fh.reset();
            fh.skip(startpos);  // 跳过块的起始位置
        } catch (Exception ex) {
            return status;
        }

        int format = sorInfo.getFormat();

        if (format == 2) {
            // 读取并验证头部
            byte[] buffer = new byte[hsize];
            try{
                fh.read(buffer, 0, hsize);
            }catch (Exception e){
                return "nok";
            }

            String mystr = new String(buffer, StandardCharsets.US_ASCII);
            if (!mystr.equals(bname + "\0")) {
                return status;
            }
        }

        // 初始化 SupParams 字典
        sorInfo.setSupParams(new SupParamsVo());

        // 处理 SupParams 字段
        try{
            status = processSupParam(fh, sorInfo);
        }
        catch (Exception e){
            return "nok";
        }

        // 读取块的剩余部分（如果需要）
        // long endpos = (long) results.get("blocks").get(bname).get("pos") + (long) results.get("blocks").get(bname).get("size");
        // fh.read(new byte[(int) (endpos - fh.position())], 0, (int) (endpos - fh.position()));

        status = "ok";
        return status;
    }

    /**
     * 处理 SupParams 字段
     */
    private static String processSupParam(BufferedInputStream fh, SorInfo sorInfo) throws IOException {
        final String bname = "SupParams";
        SupParamsVo xref = sorInfo.getSupParams();

        String[] fields = {
                "supplier", // ............. 0
                "OTDR", // ................. 1
                "OTDR S/N", // ............. 2
                "module", // ............... 3
                "module S/N", // ........... 4
                "software", // ............. 5
                "other", // ................ 6
        };

        for (String field : fields) {
            String xstr = Parts.getString(fh);

            switch (field){
                case "supplier":
                    xref.setSupplier(xstr);
                    break;
                case  "OTDR":
                    xref.setOtdr(xstr);
                    break;
                case "OTDR S/N":
                    xref.setOtdrSN(xstr);
                    break;
                case  "module":
                    xref.setModule(xstr);
                    break;
                case  "module S/N":
                    xref.setModuleSN(xstr);
                    break;
                case  "software":
                    xref.setSoftware(xstr);
                    break;
                case  "other":
                    xref.setOther(xstr);
                    break;
                default:
                    break;
            }

        }

        return "ok";
    }
}
